package com.ruoyan.service.impl;

import com.ruoyan.mapper.DepartmentMapper;
import com.ruoyan.pojo.Department;
import com.ruoyan.pojo.Employee;
import com.ruoyan.service.DepartmentService;
import com.ruoyan.service.EmployManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @Package: com.ruoyan.service
 * @ClassName: DepartmentServiceImpl
 * @Author: ruoyan1998
 * @CreateTime: 2021/5/16 16:42
 * @Description:
 */
@Service
public class DepartmentServiceImpl implements DepartmentService
{
    @Autowired
    DepartmentMapper departmentMapper;

    @Autowired
    EmployManageService employManageService;

    @Override
    public List<Department> queryDepartment()
    {
        return departmentMapper.queryDepartment();
    }

    @Override
    public Department queryDepartmentById(int id)
    {
        return departmentMapper.queryDepartmentById(id);
    }

    @Override
    public Department queryDepartmentByIdFindDid(int id)
    {
        return departmentMapper.queryDepartmentByIdFindDid(id);
    }

    @Override
    public int addDepartment(Department department)
    {
        return departmentMapper.addDepartment(department);
    }

    @Override
    public int updateDepartment(Department department)
    {
        return departmentMapper.updateDepartment(department);
    }

    @Override
    public int updateChargeManToNull(Map<String, Object> map)
    {
        return departmentMapper.updateChargeManToNull(map);
    }

    @Override
    public int deleteDepartment(int id)
    {
        //先查询部门下是否还存在有员工
        List<Employee> employees = employManageService.queryEmployeeByDid(id);

        //若部门下存在员工，则无法删除部门
        if(!employees.isEmpty())
        {
            return 0;
        }

        //部门下不存在员工，则可以删除部门
        departmentMapper.deleteDepartment(id);

        return 1;
    }


}
